<?php

/**
 * product actions.
 *
 * @package    sf_sandbox
 * @subpackage product
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class productActions extends sfActions
{
    public function executeCategories()
    {
        $categoryId = $this->getRequestParameter('id');
        $keyword = $this->getRequestParameter('keyword');
        $sort = $this->getRequestParameter('sort', "itemName");
        $order = $this->getRequestParameter('order', "asc");
        $q = "%" . $keyword . "%";

        $c = new Criteria();
        $c->add(CmsCategoriesPeer::CATEGORIES_ID, $categoryId);
        $c->add(CmsProductPeer::STATUS_CODE, Globals::STATUS_ACTIVE);

        $c1 = $c->getNewCriterion(CmsProductPeer::PRODUCT_CODE, $q, Criteria::LIKE);
        $c2 = $c->getNewCriterion(CmsProductPeer::PRODUCT_NAME, $q, Criteria::LIKE);
        $c1->addOr($c2);
        $c->add($c1);

        $cmsCategory = CmsCategoriesPeer::doSelectOne($c);
        $this->cmsCategory = $cmsCategory;

        $c = new Criteria();
        $c->add(CmsProductPeer::CATEGORIES_ID, $categoryId);
        $c->add(CmsProductPeer::STATUS_CODE, Globals::STATUS_ACTIVE);

        $c1 = $c->getNewCriterion(CmsProductPeer::PRODUCT_CODE, $q, Criteria::LIKE);
        $c2 = $c->getNewCriterion(CmsProductPeer::PRODUCT_NAME, $q, Criteria::LIKE);
        $c1->addOr($c2);
        $c->add($c1);

        $this->totalItems = CmsProductPeer::doCount($c);

        $queryItemName = "?id=" . $categoryId . "sort=itemName&order=desc&keyword=" . $keyword;
        $queryPrice = "?id=" . $categoryId . "sort=price&order=asc&keyword=" . $keyword;
        $query = "";
        if ("itemName" == $sort) {
            if ("asc" == $order) {
                $c->addAscendingOrderByColumn(CmsProductPeer::PRODUCT_NAME);
            } else {
                $queryItemName = "?id=" . $categoryId . "sort=itemName&order=asc&keyword=" . $keyword;
                $c->addDescendingOrderByColumn(CmsProductPeer::PRODUCT_NAME);
            }
            $query = $queryItemName;
        } else if ("price" == $sort) {
            if ("asc" == $order) {
                $queryPrice = "?id=" . $categoryId . "sort=price&order=desc&keyword=" . $keyword;
                $c->addAscendingOrderByColumn(CmsProductPeer::PRICE);
            } else {
                $c->addDescendingOrderByColumn(CmsProductPeer::PRICE);
            }
            $query = $queryPrice;
        }

        $limit = Globals::DISPLAY_PRODUCT_LIMIT;
        $pager = new sfPropelPager('CmsProduct', $limit);
        $pager->setCriteria($c);
        $pager->setPage($this->getRequestParameter('page', 1));
        $pager->init();

        $this->pager = $pager;
        $currentPage = $this->getRequestParameter('page', 1);

        $this->currentPage = $currentPage;

        $this->itemNumberFrom = (($currentPage - 1) * $limit) + 1;
        $this->itemNumberTo = ((($currentPage - 1) * $limit) + count($pager->getResults()));
        $this->keyword = $keyword;
        $this->query = $query;
        $this->queryItemName = $queryItemName;
        $this->queryPrice = $queryPrice;
        $this->categoryId = $categoryId;
    }

    public function executeItem()
    {
        $productId = $this->getRequestParameter('id');
        $c = new Criteria();
        $c->add(CmsProductPeer::PRODUCT_ID, $productId);
        $product = CmsProductPeer::doSelectOne($c);

        if ($product) {
            $this->cmsProduct = $product;

            $c = new Criteria();
            $c->add(CmsCategoriesPeer::CATEGORIES_ID, $product->getCategoriesId());
            $this->cmsCategory = CmsCategoriesPeer::doSelectOne($c);
        }
    }

    public function executeSearch()
    {
        $keyword = $this->getRequestParameter('keyword');
        $sort = $this->getRequestParameter('sort', "itemName");
        $order = $this->getRequestParameter('order', "asc");
        $q = "%" . $keyword . "%";

        $c = new Criteria;
        $c1 = $c->getNewCriterion(CmsProductPeer::PRODUCT_CODE, $q, Criteria::LIKE);
        $c2 = $c->getNewCriterion(CmsProductPeer::PRODUCT_NAME, $q, Criteria::LIKE);
        $c1->addOr($c2);

        //        $c->addOr(CmsProductPeer::PRODUCT_CODE, $keyword, Criteria::LIKE);
        //        $c->addOr(CmsProductPeer::PRODUCT_DESC, $keyword, Criteria::LIKE);
        //        $c->addOr(CmsProductPeer::PRODUCT_DESC_CN, $keyword, Criteria::LIKE);
        //        $c->addOr(CmsProductPeer::PRODUCT_NAME, $keyword, Criteria::LIKE);
        //        $c->addOr(CmsProductPeer::PRODUCT_NAME_CN, $keyword, Criteria::LIKE);
        $c->add($c1);

        $queryItemName = "?sort=itemName&order=desc&keyword=" . $keyword;
        $queryPrice = "?sort=price&order=asc&keyword=" . $keyword;
        if ("itemName" == $sort) {
            if ("asc" == $order) {
                $c->addAscendingOrderByColumn(CmsProductPeer::PRODUCT_NAME);
            } else {
                $queryItemName = "?sort=itemName&order=asc&keyword=" . $keyword;
                $c->addDescendingOrderByColumn(CmsProductPeer::PRODUCT_NAME);
            }
        } else if ("price" == $sort) {
            if ("asc" == $order) {
                $queryPrice = "?sort=price&order=desc&keyword=" . $keyword;
                $c->addAscendingOrderByColumn(CmsProductPeer::PRICE);
            } else {
                $c->addDescendingOrderByColumn(CmsProductPeer::PRICE);
            }
        }

        $cmsProducts = CmsProductPeer::doSelect($c);


        $this->cmsProducts = $cmsProducts;
        $this->keyword = $keyword;
        $this->queryItemName = $queryItemName;
        $this->queryPrice = $queryPrice;

        $this->setTemplate('searchResult');
    }

    public function executeAdvanceSearch()
    {
        // todo AdvanceSearch
    }

    public function executeReview()
    {
        $productId = $this->getRequestParameter('id');
        $c = new Criteria();
        $c->add(CmsProductPeer::PRODUCT_ID, $productId);
        $product = CmsProductPeer::doSelectOne($c);

        if ($product) {
            $this->cmsProduct = $product;

            $c = new Criteria();
            $c->add(CmsCategoriesPeer::CATEGORIES_ID, $product->getCategoriesId());
            $this->cmsCategory = CmsCategoriesPeer::doSelectOne($c);

            $c = new Criteria;
            $c1 = $c->getNewCriterion(CmsProductReviewPeer::STATUS_CODE, Globals::REVIEW_STATUS_ACTIVE);
            $c2 = $c->getNewCriterion(CmsProductReviewPeer::CREATED_BY, $this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
            $c1->addOr($c2);

            $c->add(CmsProductReviewPeer::PRODUCT_ID, $productId);
            $c->add($c1);

            $this->cmProductReviews = CmsProductReviewPeer::doSelect($c);
        }
    }

    public function executeSaveReview()
    {
        $cms_product_review = new CmsProductReview();

        $cms_product_review->setProductId($this->getRequestParameter('productId'));
        $cms_product_review->setRate($this->getRequestParameter('rating'));
        $cms_product_review->setReview($this->getRequestParameter('reviews'));
        $cms_product_review->setWriteBy($this->getUser()->getAttribute(Globals::SESSION_FULLNAME));
        $cms_product_review->setStatusCode(Globals::REVIEW_STATUS_PENDING);
        $cms_product_review->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID));
        $cms_product_review->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID));

        $cms_product_review->save();

        return $this->redirect('product/review?id='.$this->getRequestParameter('productId'));
    }

    public function executeWriteReview()
    {
        $productId = $this->getRequestParameter('id');
        if ($this->getUser()->isAuthenticated()) {
            $c = new Criteria();
            $c->add(CmsProductPeer::PRODUCT_ID, $productId);
            $product = CmsProductPeer::doSelectOne($c);

            if ($product) {
                $this->cmsProduct = $product;

                $c = new Criteria();
                $c->add(CmsCategoriesPeer::CATEGORIES_ID, $product->getCategoriesId());
                $this->cmsCategory = CmsCategoriesPeer::doSelectOne($c);
            }
        } else {
            $this->getUser()->setAttribute(Globals::SESSION_NEED_LOGIN, "true");
            $this->getUser()->setAttribute(Globals::SESSION_REFERER_URL, "product/writeReview?id=".$productId);
            $this->setFlash('errorMsgLogin', "<h2 id='cartEmptyText'>Please login to proceed review writing.</h2>");

            return $this->redirect('member/login');
        }
    }

    public function executeIndex()
    {
        $query = $this->getRequestParameter('p', Globals::LINK_PRODUCTS_NEW);
        $sorting = $this->getRequestParameter('disp_order', Globals::DISP_ORDER_DATE_ADDED_NEW_TO_OLD);

        $c = new Criteria();

        if (Globals::LINK_PRODUCTS_NEW == $query) {
            $c->addDescendingOrderByColumn(CmsProductPeer::UPDATED_ON);
            $c->setLimit(Globals::QUERY_NEW_PRODUCT_LIMIT);
        } else if (Globals::LINK_PRODUCTS_SPECIAL == $query) {
            $c->add(CmsProductPeer::SPECIAL_ITEM, Globals::DB_YES);
            $c->addDescendingOrderByColumn(CmsProductPeer::UPDATED_ON);
        } else if (Globals::LINK_PRODUCTS_ALL == $query) {
            $c->addDescendingOrderByColumn(CmsProductPeer::UPDATED_ON);
        } else if (Globals::LINK_PRODUCTS_REVIEWS == $query) {
            $c->add(CmsProductPeer::LAST_REVIEWS, null, Criteria::ISNOTNULL);
            $c->addDescendingOrderByColumn(CmsProductPeer::UPDATED_ON);
        }

        if (Globals::DISP_ORDER_PRODUCT_NAME == $sorting) {
            $c->addAscendingOrderByColumn(CmsProductPeer::PRODUCT_NAME);
        } else if (Globals::DISP_ORDER_PRICE_LOW_TO_HIGH == $sorting) {
            $c->addAscendingOrderByColumn(CmsProductPeer::PRICE);
        } else if (Globals::DISP_ORDER_PRICE_HIGH_TO_LOW == $sorting) {
            $c->addDescendingOrderByColumn(CmsProductPeer::PRICE);
        } else if (Globals::DISP_ORDER_DATE_ADDED_NEW_TO_OLD == $sorting) {
            $c->addDescendingOrderByColumn(CmsProductPeer::CREATED_ON);
        } else if (Globals::DISP_ORDER_DATE_ADDED_OLD_TO_NEW == $sorting) {
            $c->addAscendingOrderByColumn(CmsProductPeer::CREATED_ON);
        }

        $c->add(CmsProductPeer::STATUS_CODE, Globals::STATUS_ACTIVE);
        $this->totalItems = CmsProductPeer::doCount($c);

        $limit = Globals::DISPLAY_PRODUCT_LIMIT;
        $pager = new sfPropelPager('CmsProduct', $limit);
        $pager->setCriteria($c);
        $pager->setPage($this->getRequestParameter('page', 1));
        $pager->init();

        $this->pager = $pager;
        $currentPage = $this->getRequestParameter('page', 1);

        $this->currentPage = $currentPage;

        $this->itemNumberFrom = (($currentPage - 1) * $limit) + 1;
        $this->itemNumberTo = ((($currentPage - 1) * $limit) + count($pager->getResults()));
        $this->query = $query;
        $this->sorting = $sorting;
    }
}